/**
 * Created by pankajmishra on 16/01/15.
 */
define(['jquery',
        'module/notice/item',
        'module/home/customEvent',
        'module/home/showUpdateBox',
        'module/home/more'],
    function ($, item, customEvent, updateBox, more) {
        // we need to create update box also.


        var mainUpdate = function (options) {

            // load things for update in main arena.
            var thisAlways = this, pageDS = {maxLastSeenId: 0, minLastSeenId: 0, pageSize: 10},
                template = require('module/template/homeTemplate'), moreAdded = false;
            $('.main-content').empty();
            $('.main-content').append(template[options.homeTemplate]);
            $('.main-content').find('.agenda-form').hide();

            // bind document with event of appending and pre pending.


            $(document).once(options.eventName, function (event) {
                thisAlways.takeUpdate(event.append);
            });

            this.takeUpdate = function (append) {

                if (append) {
                    var dynaClass = 'js_'+options.eventName, mainUpdate = $('.main-content').find('.update-area'),
                        loading = $(template['home-loading']);
                    mainUpdate.addClass(dynaClass);
                    mainUpdate.append(loading);
                    /**
                     * update on home page.
                     */
                    $.ajax({
                        url: options.url, data: {
                            lastSeenId: pageDS.minLastSeenId,
                            limit: pageDS.pageSize,
                            after: ((pageDS.minLastSeenId == 0) ? true : false)
                        }, dataType: "json"
                    }).done(function (data) {
                        $.each(data, function (index, value) {
                            options.append(value);
                            if (pageDS.maxLastSeenId < value.upid) {
                                pageDS.maxLastSeenId = value.upid;
                            }
                            pageDS.minLastSeenId = value.upid;
                        });
                        more(data.length, pageDS.pageSize, options.eventName);
                        loading.remove();
                    });

                } else {

                    /**
                     * update on home page.
                     */
                    $.ajax({
                        url:  options.url, data: {
                            lastSeenId: pageDS.maxLastSeenId,
                            limit: pageDS.pageSize,
                            after: true
                        }, dataType: "json"
                    }).done(function (data) {
                        $.each(data, function (index, value) {
                            options.prepend(value);
                            if (pageDS.maxLastSeenId < value.upid) {
                                pageDS.maxLastSeenId = value.upid;
                            }
                        });
                    });
                }
            };

            $.event.trigger({
                type: options.eventName,
                append: true
            });
            // initialize update box.
            // updateBox($("#updateInput"), {updateType : '1'});

        };
        return {
            /**
             * { homeTemplate : #, item :#, url
             */
            render : mainUpdate,
            create : updateBox
        };
    });